//
//  NSManagedObject+Fetching.m
//  LibraryStorm
//
//  Created by Anh Nguyen on 4/1/14.
//  Copyright (c) 2014 3ATeam. All rights reserved.
//

#import "NSManagedObject+Fetching.h"
#import "LSCoreDataController.h"

@implementation NSManagedObject (Fetching)

+ (NSEntityDescription *)entityDescriptionInContext:(NSManagedObjectContext *)context;
{
    return [NSEntityDescription entityForName:NSStringFromClass(self) inManagedObjectContext:context];
}

+ (NSArray *)getAllObjectsResetContext:(BOOL)isReset fetchLimit:(int)limit;
{
    NSManagedObjectContext *context = [[LSCoreDataController sharedInstance] backgroundManagedObjectContext];
    return [self getAllObjectsInContext:context resetContext:isReset fetchLimit:limit];
}

+ (NSArray *)getAllObjectsInContext:(NSManagedObjectContext *)context resetContext:(BOOL)isReset fetchLimit:(int)limit;
{
    return [self getAllObjectsInContext:context withSortDescriptor:nil withPredicate:nil resetContext:isReset fetchLimit:limit];
}

+ (NSArray *)getAllObjectsInContext:(NSManagedObjectContext *)context withSortDescriptor:(NSSortDescriptor *)sortDescriptor resetContext:(BOOL)isReset fetchLimit:(int)limit
{
    return [self getAllObjectsInContext:context withSortDescriptor:sortDescriptor withPredicate:nil resetContext:isReset fetchLimit:limit];
}

+ (NSArray *)getAllObjectsInContext:(NSManagedObjectContext *)context withSortDescriptor:(NSSortDescriptor *)sortDescriptor withPredicate:(NSPredicate *)predicate resetContext:(BOOL)isReset fetchLimit:(int)limit
{
    __block NSArray *results = nil;
    [context performBlockAndWait:^{
        if (isReset) {
            [context reset];
        }
        
        NSError *error = nil;
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        if (limit > 0) {
           [request setFetchLimit:limit];
        }
        
        [request setEntity:[self entityDescriptionInContext:context]];
        if (sortDescriptor) {
            [request setSortDescriptors:@[sortDescriptor]];
        }
        
        if (predicate) {
            [request setPredicate:predicate];
        }
        
        results = [context executeFetchRequest:request error:&error];
    }];
    
    return results;
}

+ (NSManagedObject *)findObjectInContext:(NSManagedObjectContext *)context withPredicate:(NSPredicate *)predicate
{
    NSArray *results = [self getAllObjectsInContext:context withSortDescriptor:nil withPredicate:predicate resetContext:NO fetchLimit:0];
    if (results && results.count > 0) {
        return results[0];
    }
    
    return nil;
}

- (void)setObjectInContext:(NSManagedObjectContext *)context withValue:(id)value forKey:(NSString *)key
{
    [self setValue:value forKey:key];
    [context performBlockAndWait:^{
        NSError *error = nil;
        BOOL saved = [context save:&error];
        if (!saved) {
            // do some real error handling
            NSLog(@"Could not save NSManagedObject due to %@", error);
        }
        [[LSCoreDataController sharedInstance] saveMasterContext];
    }];
}
@end
